package com.heliang.service.info.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.heliang.common.FileLog;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.dataaccess.bean.InfoVersionBean;
import com.heliang.dataaccess.dao.InfoDao;
import com.heliang.dataaccess.dao.InfoVersionDao;
import com.heliang.dataaccess.dao.impl.InfoDaoImpl;
import com.heliang.dataaccess.dao.impl.InfoVersionDaoImpl;
import com.heliang.protocol.requestBean.ReqBodyInfoBean;
import com.heliang.protocol.responseBean.RspBodyInfoBean;
import com.heliang.protocol.service.InfoProtocolImpl;
import com.heliang.service.BaseService;
import com.heliang.service.info.InfoService;
import com.heliang.util.AppUtil;
import com.heliang.util.Constants;
import com.heliang.util.Encry;
import com.hl.component.exception.ErrorCode;
import com.hl.component.exception.ServiceException;
import com.hl.component.exception.handle.ExceptionHandle;

public class InfoServiceImpl extends BaseService implements InfoService {

    private InfoDao infoDaoImpl;

    private ReqBodyInfoBean reqBodyInfoBean;

    private RspBodyInfoBean rspBodyInfoBean;

    private InfoProtocolImpl infoProtocolImpl;

    private Context context;

    private String url = "";

    public InfoServiceImpl(Context context) {
        this.context = context;
        infoDaoImpl = new InfoDaoImpl(context);
        rspBodyInfoBean = new RspBodyInfoBean();
        reqBodyInfoBean = new ReqBodyInfoBean();
        infoProtocolImpl = new InfoProtocolImpl();
    }

    @Override
    public List<InfoBean> getInfoBeanByCatId(int catId, int type) throws ServiceException {
        List<InfoBean> infoBeans = new ArrayList<InfoBean>();
        try {
            infoBeans = infoDaoImpl.query(type, catId);
            return infoBeans;
        } catch (Exception e) {
            ExceptionHandle.throwServiceException(e, ErrorCode.SERVICE_QUERY_ERROR_004, "InfoServiceImpl.getInfoBeanByCatId " + "根据资讯类别查询资讯失败！");
            return null;
        }
    }

    @Override
    public void updateData() throws ServiceException {
        try {
            /* 先查本地询资讯的版本 */
            int oldInfoVer;
            InfoVersionDao infoVerDao = new InfoVersionDaoImpl(context);
            oldInfoVer = infoVerDao.getInfoVersionBeanById(reqBodyInfoBean.getCatId()).getVer();

            /* 查询服务器最新的资讯版本 */
            int newInfoVer;
            String keyvalue = Encry.md5s(Constants.SITE_ID + Constants.SECKEY);
            reqBodyInfoBean.setKeyValue(keyvalue);
            reqBodyInfoBean.setSiteId(Constants.SITE_ID);
            reqBodyInfoBean.setVerInfo(oldInfoVer);
            reqBodyInfoBean.setCatId(reqBodyInfoBean.getCatId());
            url = Constants.ACCESS_SERVICE_LINK + Constants.INTERFACE_INFO;
            try {
                rspBodyInfoBean = (RspBodyInfoBean) InfoProtocolImpl.dataProcess(reqBodyInfoBean, url);
            } catch (Exception e) {
               return;
            }
           
            /* 比较服务器版本和本地版本 */
            newInfoVer = rspBodyInfoBean.getVer();
            if (oldInfoVer < newInfoVer) {
                List<InfoBean> infos = rspBodyInfoBean.getInfoList();

                Integer[] infoIds = new Integer[infos.size()];
                for (int i = 0; i < infoIds.length; i++) {
                    InfoBean infoBean = infos.get(i);
                    infoIds[i] = infoBean.getId();
                }
                Integer[] infoDelIds = rspBodyInfoBean.getDelIds();
                if (infoDelIds != null) {
                    infoDaoImpl.delete(0, reqBodyInfoBean.getCatId(), AppUtil.mergeIntArray(infoIds, infoDelIds));
                } else {
                    infoDaoImpl.delete(0, reqBodyInfoBean.getCatId(), infoIds);// 删除所有下发的记录
                }
                infoDaoImpl.saveInfos(infos);// 插入update的记录

                // 保证首页只有20条记录
                int n = infoDaoImpl.queryInfoCount(0, reqBodyInfoBean.getCatId());
                if (n > 20) {
                    infoDaoImpl.deleteOldest(0, reqBodyInfoBean.getCatId(), n - 20);
                }

                InfoVersionBean infoVersionBean = new InfoVersionBean();
                
                infoVersionBean.setCatId(reqBodyInfoBean.getCatId());

                infoVersionBean.setVer(newInfoVer);// 更新版本号
                infoVerDao.updateInfoVersionBean(infoVersionBean);
            }
        } catch (Exception e) {
            ExceptionHandle.throwServiceException(e, ErrorCode.SERVICE_QUERY_ERROR_004, "InfoServiceImpl.updateData " + "更新资讯失败！");
        }
    }

    /**
     * 点击加载更多
     * @throws ServiceException 
     */
    @Override
    public void getMoreData() throws ServiceException {
        String url = Constants.ACCESS_SERVICE_LINK + Constants.INTERFACE_INFO;
        try {
            rspBodyInfoBean = (RspBodyInfoBean) InfoProtocolImpl.dataProcess(reqBodyInfoBean, url);
        } catch (Exception e) {
            ExceptionHandle.throwServiceException(e, ErrorCode.SERVICE_QUERY_ERROR_004, "InfoServiceImpl.getMoreData " + "点击加载更多！");
        }

    }

    @Override
    public boolean updateReadStatus(int id, boolean isRead) {
        try {
            InfoBean infoBean = new InfoBean();
            infoBean.setId(id);
            infoBean.setRead(isRead);
            infoDaoImpl.updateInfoBean(infoBean);
        } catch (Exception e) {
            e.printStackTrace();
            FileLog.log(Constants.TAG + e.getMessage() + "-- in InfoServiceImpl.updateReadStatus --");
            return false;
        }

        return false;
    }

    public ReqBodyInfoBean getReqBodyInfoBean() {
        return reqBodyInfoBean;
    }

    public void setReqBodyInfoBean(ReqBodyInfoBean reqBodyInfoBean) {
        this.reqBodyInfoBean = reqBodyInfoBean;
    }

    public RspBodyInfoBean getRspBodyInfoBean() {
        return rspBodyInfoBean;
    }

    public void setRspBodyInfoBean(RspBodyInfoBean rspBodyInfoBean) {
        this.rspBodyInfoBean = rspBodyInfoBean;
    }

}
